package com.flejay.dreamrecruiter.tools.converter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import com.flejay.dreamrecruiter.model.CurriculumVitae;

/**
 * Tool qui converti un fichier Texte en CurriculumVitae
 * 
 * @author Victor Fleurant
 * 
 */
public final class TextFileConverterTool {

	private TextFileConverterTool() {
		super();
	}

	/**
	 * Converti le fichier en CV
	 * 
	 * @param file
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static CurriculumVitae convert(final File file)
			throws FileNotFoundException, IOException {
		assert file == null : "file could not be null";
		assert !file.isFile() : "file is not a file";
		String cvContent = extractText(file);
		CurriculumVitae cv = CurriculumVitae.getNewInstance();
		cv.setContent(cvContent);
		cv.setName(file.getName());
		return cv;
	}

	/**
	 * extrait le texte 
	 * 
	 * @param file
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static String extractText(final File file) throws FileNotFoundException, IOException {
		String res = "";
		BufferedReader br = new BufferedReader(new FileReader(file));
		try {
			StringBuilder sb = new StringBuilder();
			String line = br.readLine();
			while (line != null) {
				sb.append(line);
				sb.append(System.lineSeparator());
				line = br.readLine();
			}
			res = sb.toString();
		} finally {
			br.close();
		}
		return res;
	}

}
